﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AEM.Data;
using AEM.Common;

namespace AEM.Controls
{
    /// <summary>
    /// RunTime.xaml 的交互逻辑
    /// </summary>
    public partial class RunTime : UserControl
    {
        public RunTime()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(RunTime_Loaded);
            this.datefrom.ValueChanged += new EventHandler(datefrom_SelectedDateChanged);
            this.dateto.ValueChanged += new EventHandler(dateto_SelectedDateChanged);
            this.btn_select.Click += new RoutedEventHandler(btn_select_Click);
            this.btn_reset.Click += new RoutedEventHandler(btn_reset_Click);
        }

        void btn_reset_Click(object sender, RoutedEventArgs e)
        {
            this.liftNum.SelectedItem = null;
            this.datefrom.Value = DateTime.Now;
            this.dateto.Value = DateTime.Now;
        }

        void RunTime_Loaded(object sender, RoutedEventArgs e)
        {
            if (null != Config.LiftInftList)
                this.liftNum.ItemsSource = Config.LiftInftList;
            this.liftNum.DisplayMemberPath = "LiftSite";
            this.liftNum.Items.Refresh();

            DateTime s = DataLogHelper.GetEarliestTime();
            if (null != s)
            {
                this.datefrom.DisplayDateStart = s;
                this.dateto.DisplayDateStart = s;
            }
            else
            {
                this.datefrom.DisplayDateStart = DateTime.Now;
            }
            this.datefrom.DisplayDateEnd = DateTime.Now;
            this.dateto.DisplayDateEnd = DateTime.Now;
        }

        void dateto_SelectedDateChanged(object sender, EventArgs e)
        {
            this.datefrom.DisplayDateEnd = this.dateto.Value;
        }

        void datefrom_SelectedDateChanged(object sender, EventArgs e)
        {
            this.dateto.DisplayDateStart = this.datefrom.Value;
        }

        void btn_select_Click(object sender, RoutedEventArgs e)
        {
            this.btn_reset.IsEnabled = false;
            this.msg.Text = "";
            if (null == this.datefrom.Value)
            {
                this.msg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("DateFrom") as string);
                this.btn_reset.IsEnabled = true ; return;
            }
            if (null == this.dateto.Value)
            {
                this.msg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("DateTo") as string);
                this.btn_reset.IsEnabled = true ; return;
            }
            DateTime s = this.datefrom.Value.Value;
            DateTime t = this.dateto.Value.Value;

            this.msg.Text = this.TryFindResource("Searching") as string;
            if (null != this.liftNum.SelectedItem)
            {
                RunTimeItem rti = CreateItem(this.liftNum.SelectedItem as Data.Model.LiftInfo, s, t);
                this.listStatusRatio.ItemsSource = new List<RunTimeItem>() { rti };
            }
            else
            {
                List<RunTimeItem> list = new List<RunTimeItem>();
                foreach (Data.Model.LiftInfo r in liftNum.Items)
                {
                    list.Add(CreateItem(r, s, t));
                }
                this.listStatusRatio.ItemsSource = list;
            }
            this.btn_reset.IsEnabled = true;
            this.msg.Text = this.TryFindResource("SearchOK") as string;
        }

        private RunTimeItem CreateItem(Data.Model.LiftInfo liftInfo, DateTime s, DateTime t)
        {
            try
            {
                RunTimeItem i = new RunTimeItem();
                i.ShortName = liftInfo.ShortName;
                i.LiftInfo = liftInfo;
                string time, times, doors;
                DataLogHelper.GetRunTime(liftInfo, s, t, out time, out times, out doors);
                i.RunTime = time;
                i.RunTimes = times;
                i.Doors = doors;
                return i;
            }
            catch
            {
                return null;
            }
        }
    }

    public class RunTimeItem
    {
        public string ShortName { get; set; }
        public Data.Model.LiftInfo LiftInfo { get; set; }
        public string RunTime { get; set; }
        public string RunTimes { get; set; }
        public string Doors { get; set; }

    }
}
